package com.mxpio.erp.plm.controller;

import com.mxpio.erp.common.plm.entity.ProjectInfoStatusHistory;
import com.mxpio.erp.common.plm.service.ProjectInfoStatusHistoryService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "ProjectInfoStatusHistoryController", description = "项目状态历史")
@RestController
@RequestMapping("/erp/plm/pro/info/status/history/")
public class ProjectInfoStatusHistoryController {

    @Autowired
    private ProjectInfoStatusHistoryService projectInfoStatusHistoryService;

    @GetMapping("list")
    @Operation(summary = "状态历史列表", description = "状态历史列表（无分页）", method = "GET")
    public Result<List<ProjectInfoStatusHistory>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<ProjectInfoStatusHistory> list = projectInfoStatusHistoryService.list(ProjectInfoStatusHistory.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "状态历史列表", description = "状态历史列表（分页）", method = "GET")
    public Result<Page<ProjectInfoStatusHistory>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<ProjectInfoStatusHistory> pageResult = projectInfoStatusHistoryService.listPage(ProjectInfoStatusHistory.class, page, criteria);
        return Result.OK(pageResult);
    }

    @GetMapping("list/{proCode}")
    @Operation(summary = "根据项目编码-获取项目状态信息", description = "根据项目编码-获取项目状态信息", method = "GET")
    public Result<List<ProjectInfoStatusHistory>> getById(@PathVariable(name = "proCode", required = true) String proCode) {
        Criteria criteria = Criteria.create().addCriterion("proCode", Operator.EQ, proCode);
        List<ProjectInfoStatusHistory> list = projectInfoStatusHistoryService.list(ProjectInfoStatusHistory.class, criteria);
        return Result.OK(list);
    }

    @RequestMapping("add")
    @Operation(summary = "保存项目状态信息", description = "保存项目状态信息")
    public Result<ProjectInfoStatusHistory> add(@RequestBody ProjectInfoStatusHistory history) {
        projectInfoStatusHistoryService.save(history);
        return Result.OK(history);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑项目状态信息", description = "编辑项目状态信息", method = "PUT")
    public Result<ProjectInfoStatusHistory> edit(@RequestBody ProjectInfoStatusHistory history) {
        projectInfoStatusHistoryService.update(history);
        return Result.OK(history);
    }

    @DeleteMapping("delete")
    @Operation(summary = "通过id删除", description = "通过id删除", method = "DELETE")
    public Result<ProjectInfoStatusHistory> delete(@RequestParam(name="id",required=true) String id) {
        projectInfoStatusHistoryService.delete(id,ProjectInfoStatusHistory.class);
        return Result.OK();
    }
}
